package com.reebake.ideal.message.websocket.handler;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.reebake.ideal.message.core.MessageMediumTypeConstants;
import com.reebake.ideal.message.core.MessageStatusConstants;
import com.reebake.ideal.message.core.MessageTypeConstants;
import com.reebake.ideal.message.entity.MessageDeliveryDTO;
import com.reebake.ideal.message.entity.MessageDeliveryFeedbackDTO;
import com.reebake.ideal.message.event.MessageDeliveryEvent;
import com.reebake.ideal.message.event.MessageDeliveryFeedbackEvent;
import com.reebake.ideal.message.websocket.core.WebsocketMessagePublisher;
import com.reebake.ideal.message.websocket.entity.WebsocketMessageTransferDTO;
import com.reebake.ideal.message.websocket.entity.WebsocketMessageTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class WebsocketDeliveryEventListener {

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;
    @Autowired
    private WebsocketMessagePublisher websocketMessagePublisher;

    @EventListener
    public void handle(MessageDeliveryEvent messageDeliveryEvent) {
        MessageDeliveryDTO messageDeliveryDTO = messageDeliveryEvent.getMessageDeliveryDTO();
        List<String> supportMediumTypes = CollectionUtil.newArrayList(MessageMediumTypeConstants.APP);
        if(!supportMediumTypes.contains(messageDeliveryDTO.getMediumType())) {
            return;
        }
        String title = messageDeliveryDTO.getTitle();
        String senderName = messageDeliveryDTO.getSenderName();
        String senderAvatar = messageDeliveryDTO.getSenderAvatar();
        Boolean isBatchSend = messageDeliveryDTO.getIsBatchSend();
        Boolean enableDelivery = messageDeliveryDTO.getEnableDelivery();
        String receiverIds = messageDeliveryDTO.getReceiverIds();
        String messageHistoryId = messageDeliveryDTO.getMessageHistoryId();
        String content = messageDeliveryDTO.getContent();

        WebsocketMessageTransferDTO websocketMessageTransferDTO = new WebsocketMessageTransferDTO();
        websocketMessageTransferDTO.setContent(content);
        websocketMessageTransferDTO.setReceiver(receiverIds);
        websocketMessageTransferDTO.setTitle(title);
        websocketMessageTransferDTO.setType(WebsocketMessageTypeEnum.TIP);
        websocketMessageTransferDTO.setSender(senderName);
        websocketMessageTransferDTO.setDestination(messageDeliveryDTO.getChannelId());
        if(MessageTypeConstants.CMD.equals(messageDeliveryDTO.getMessageType())) {
            websocketMessageTransferDTO.setType(WebsocketMessageTypeEnum.CMD);
        }else {
            websocketMessageTransferDTO.setType(WebsocketMessageTypeEnum.TIP);
        }
        websocketMessagePublisher.sendMessage(websocketMessageTransferDTO);

        MessageDeliveryFeedbackDTO messageDeliveryFeedbackDTO = new MessageDeliveryFeedbackDTO();
        messageDeliveryFeedbackDTO.setMessageHistoryId(messageHistoryId);
        messageDeliveryFeedbackDTO.setDeliveryTime(DateUtil.date());
        messageDeliveryFeedbackDTO.setDeliveryStatus(MessageStatusConstants.DELIVERED);
        messageDeliveryFeedbackDTO.setSenderName(senderName);
        messageDeliveryFeedbackDTO.setSenderAvatar(senderAvatar);

        applicationEventPublisher.publishEvent(new MessageDeliveryFeedbackEvent(this, messageDeliveryFeedbackDTO));
    }
}
